package com.ruoyi.web.controller.fs;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.fs.domain.FsLayerTeam;
import com.ruoyi.fs.service.IFsLayerTeamService;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fs.domain.FsLawyer;
import com.ruoyi.fs.service.IFsLawyerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 律师信息Controller
 *
 * @author hero
 * @date 2024-09-01
 */
@RestController
@RequestMapping("/fs/lawyer")
public class FsLawyerController extends BaseController {
    @Autowired
    private IFsLawyerService fsLawyerService;
    @Autowired
    private IFsLayerTeamService fsLayerTeamService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询律师信息列表
     */
    @PreAuthorize("@ss.hasPermi('fs:lawyer:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsLawyer fsLawyer) {
        startPage();
        List<FsLawyer> list = fsLawyerService.selectFsLawyerList(fsLawyer);
        for (int i = 0; i < list.size(); i++) {
            FsLawyer lawyer = list.get(i);
            FsLayerTeam query = new FsLayerTeam();
            query.setLsid(lawyer.getId());
            List<FsLayerTeam> teams = fsLayerTeamService.selectFsLayerTeamList(query);
            String tjyNames = "";
            if (!teams.isEmpty()) {
                for (int j = 0; j < teams.size(); j++) {
                    FsLayerTeam team = teams.get(j);
                    SysUser user = sysUserService.selectUserById(team.getTjyid());
                    tjyNames += user.getNickName();
                    if (j < (teams.size() - 1)) {
                        tjyNames += ",";
                    }
                }
            }
            lawyer.setTjyNames(tjyNames);
        }
        return getDataTable(list);
    }

    /**
     * 导出律师信息列表
     */
    @PreAuthorize("@ss.hasPermi('fs:lawyer:export')")
    @Log(title = "律师信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FsLawyer fsLawyer) {
        List<FsLawyer> list = fsLawyerService.selectFsLawyerList(fsLawyer);
        ExcelUtil<FsLawyer> util = new ExcelUtil<FsLawyer>(FsLawyer.class);
        util.exportExcel(response, list, "律师信息数据");
    }

    /**
     * 获取律师信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('fs:lawyer:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        AjaxResult ajax = AjaxResult.success();
        FsLawyer lawyer = fsLawyerService.selectFsLawyerById(id);
        FsLayerTeam query = new FsLayerTeam();
        query.setLsid(id);
        List<Long> tjyIds = new ArrayList<>();
        List<FsLayerTeam> teams = fsLayerTeamService.selectFsLayerTeamList(query);
        String tjyNames = "";
        if (!teams.isEmpty()) {
            for (int j = 0; j < teams.size(); j++) {
                FsLayerTeam team = teams.get(j);
                tjyIds.add(team.getTjyid());
                SysUser user = sysUserService.selectUserById(team.getTjyid());
                tjyNames += user.getNickName();
                if (j < (teams.size() - 1)) {
                    tjyNames += ",";
                }
            }
        }
        lawyer.setTjyNames(tjyNames);
        lawyer.setTjyIds(tjyIds);
        SysUser query2 = new SysUser();
        query2.setRoleId(101L);
        List<SysUser> tjys = sysUserService.selectUserList(query2);
        ajax.put("tjys", tjys);
        ajax.put(AjaxResult.DATA_TAG, lawyer);
        return ajax;
    }

    /**
     * 新增律师信息
     */
    @PreAuthorize("@ss.hasPermi('fs:lawyer:add')")
    @Log(title = "律师信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsLawyer fsLawyer) {
        return toAjax(fsLawyerService.insertFsLawyer(fsLawyer));
    }

    /**
     * 修改律师信息
     */
    @PreAuthorize("@ss.hasPermi('fs:lawyer:edit')")
    @Log(title = "律师信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsLawyer fsLawyer) {
        List<Long> tjyIds = fsLawyer.getTjyIds();
        FsLayerTeam query = new FsLayerTeam();
        query.setLsid(fsLawyer.getId());
        List<FsLayerTeam> teams = fsLayerTeamService.selectFsLayerTeamList(query);
        if (!teams.isEmpty()) {
            for (int i = 0; i < teams.size(); i++) {
                fsLayerTeamService.deleteFsLayerTeamById(teams.get(i).getId());
            }
        }
        for (int i = 0; i < tjyIds.size(); i++) {
            FsLayerTeam team = new FsLayerTeam();
            team.setLsid(fsLawyer.getId());
            team.setTjyid(tjyIds.get(i));
            fsLayerTeamService.insertFsLayerTeam(team);
        }
        return toAjax(fsLawyerService.updateFsLawyer(fsLawyer));
    }

    /**
     * 删除律师信息
     */
    @PreAuthorize("@ss.hasPermi('fs:lawyer:remove')")
    @Log(title = "律师信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(fsLawyerService.deleteFsLawyerByIds(ids));
    }
}
